package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.common.utils.security.Md5Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PersonnelMapper;
import com.ruoyi.system.domain.Personnel;
import com.ruoyi.system.service.IPersonnelService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.ObjectUtils;

import javax.validation.Validator;

/**
 * 用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-27
 */
@Service
public class PersonnelServiceImpl implements IPersonnelService
{
    private static final Logger log = LoggerFactory.getLogger(PersonnelServiceImpl.class);

    @Autowired
    private PersonnelMapper personnelMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询用户
     * 
     * @param id 用户主键
     * @return 用户
     */
    @Override
    public Personnel selectPersonnelById(Long id)
    {
        return personnelMapper.selectPersonnelById(id);
    }

    /**
     * 查询用户列表
     * 
     * @param personnel 用户
     * @return 用户
     */
    @Override
    public List<Personnel> selectPersonnelList(Personnel personnel)
    {
        return personnelMapper.selectPersonnelList(personnel);
    }

    /**
     * 新增用户
     * 
     * @param personnel 用户
     * @return 结果
     */
    @Override
    public int insertPersonnel(Personnel personnel)
    {
        String password = "123456";
        personnel.setPassword(Md5Utils.hash(personnel.getUsername() + password));
        personnel.setCreateTime(DateUtils.getNowDate());
        return personnelMapper.insertPersonnel(personnel);
    }

    /**
     * 修改用户
     * 
     * @param personnel 用户
     * @return 结果
     */
    @Override
    public int updatePersonnel(Personnel personnel)
    {
        personnel.setUpdateTime(DateUtils.getNowDate());
        return personnelMapper.updatePersonnel(personnel);
    }

    /**
     * 批量删除用户
     * 
     * @param ids 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deletePersonnelByIds(String ids)
    {
        return personnelMapper.deletePersonnelByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户信息
     * 
     * @param id 用户主键
     * @return 结果
     */
    @Override
    public int deletePersonnelById(Long id)
    {
        return personnelMapper.deletePersonnelById(id);
    }

    /**
     * 导入用户数据
     *
     * @return 结果
     */
    @Override
    public String importPersonnel(List<Personnel> personnelList, boolean isUpdateSupport, String loginName)
    {
        if (StringUtils.isNull(personnelList) || personnelList.size() == 0)
        {
            throw new ServiceException("导入人员数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Personnel personnel : personnelList)
        {
            try
            {
                // 验证是否存在这个人员
                Personnel p = personnelMapper.selectPersonnelByUsername(personnel.getUsername());
                if (StringUtils.isNull(p))
                {
                    BeanValidators.validateWithException(validator, personnel);
                    String password = "123456";
                    personnel.setPassword(Md5Utils.hash(personnel.getUsername() + password));
                    personnelMapper.insertPersonnel(personnel);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + personnel.getUsername() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, personnel);
                    if (ObjectUtils.isEmpty(personnel.getId())) {
                        throw new ServiceException("用户编号不能为空");
                    }
                    personnel.setUpdateTime(new Date());
                    personnelMapper.updatePersonnel(personnel);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + personnel.getUsername() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + personnel.getUsername() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + personnel.getUsername() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
